package com.srpingde.hhh;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class GengGaiMiMaActivity extends Activity {
	EditText oldPassword;
	EditText newPassword;
	EditText password_confirm;
	HHHIml weiQuIml;
	TextView title;
	ImageView back;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_dialog);
		weiQuIml = new HHHIml();
		oldPassword = (EditText) findViewById(R.id.dialog_password_old);
		oldPassword.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = oldPassword.getSelectionStart();
                selectionEnd = oldPassword.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    oldPassword.setText(s);
                    oldPassword.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(GengGaiMiMaActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
		newPassword = (EditText) findViewById(R.id.dialog_password_new);
		newPassword.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = newPassword.getSelectionStart();
                selectionEnd = newPassword.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    newPassword.setText(s);
                    newPassword.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(GengGaiMiMaActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
		password_confirm = (EditText) findViewById(R.id.dialog_password_confirm);
		password_confirm.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = password_confirm.getSelectionStart();
                selectionEnd = password_confirm.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    password_confirm.setText(s);
                    password_confirm.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(GengGaiMiMaActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
		title = (TextView) findViewById(R.id.title);
		layout();
	}
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.genggai_btn:
			if(!oldPassword.getText().toString().equals("") && !newPassword.getText().toString().equals("")){
				if(newPassword.getText().toString().equals(password_confirm.getText().toString())){
					final String url = HHHInterface.httpHeader + "api.php?act=updatpwd&uid="+HhhApp.getInstance().getUid()+"&pwd="+newPassword.getText().toString().trim();
					Log.e("mytag", url);
					weiQuIml.gengGaiMiMa(url,GengGaiMiMaActivity.this,new AsyncHttpResponseHandler(){
						@Override
						public void onSuccess(final String content) {
							super.onSuccess(content);
							Log.e("mytag", content);
							try {
								JSONObject jsonObject = new JSONObject(content);
								if(jsonObject.getBoolean("return")){
									new AlertDialog.Builder(GengGaiMiMaActivity.this).setTitle("提示").setMessage("更改密码成功").setPositiveButton("确定", new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											// TODO Auto-generated method stub
											finish();	
										}
									}).create().show();
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}else{
					HhhApp.getInstance().showDialog(GengGaiMiMaActivity.this, "两次密码不一样");	
				}
			}else{
				HhhApp.getInstance().showDialog(GengGaiMiMaActivity.this, "请完善信息");	
			}
			break;
		}
	}
	 private void layout() {
			// 头的高度
			RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams) title.getLayoutParams();
			titleLayoutParams.height = HhhApp.getInstance().title_h();
			title.setLayoutParams(titleLayoutParams);
			//返回按钮
			ImageView back = (ImageView) findViewById(R.id.back);
			RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
			backLayoutParams.height = HhhApp.getInstance().title_h();
			back.setLayoutParams(backLayoutParams);
		}
}
